"use client";

import { categoryApi } from '@/constants';
import { get, post, put, patch, del } from '@/util/api';

// 分类基本信息接口
export interface CategoryBase {
  name: string;
  parentId?: number | null;
  description?: string;
  icon?: string;
  sort?: number;
  isActive?: number;
}

// 分类响应接口
export interface Category extends CategoryBase {
  id: number;
  articleCount: number;
  createdAt: string;
  updatedAt: string;
  parent?: Category;
  children?: Category[];
}

// 分类树节点接口
export interface CategoryTreeNode extends Category {
  children: CategoryTreeNode[];
}

/**
 * 分类管理服务类
 * 封装所有分类相关的API调用
 */
class CategoryService {
  /**
   * 获取所有分类
   * @param params 查询参数
   * @returns Promise<Category[]>
   */
  async getAllCategories(params?: {
    isActive?: number;
    parentId?: number | null;
  }): Promise<Category[]> {
    try {
      const response = await get(categoryApi.LIST, {params});
      return (response.data || response) as unknown as Category[];
    } catch (error) {
      console.error('获取分类列表失败:', error);
      throw error;
    }
  }

  /**
   * 获取分类树
   * @param params 查询参数
   * @returns Promise<CategoryTreeNode[]>
   */
  // async getCategoryTree(params?: {
  //   isActive?: number;
  //   depth?: number;
  // }): Promise<CategoryTreeNode[]> {
  //   try {
  //     const response = await get(categoryApi.TREE, {params});
  //     return response.data || response;
  //   } catch (error) {
  //     console.error('获取分类树失败:', error);
  //     throw error;
  //   }
  // }

  /**
   * 获取分类详情
   * @param id 分类ID
   * @returns Promise<Category>
   */
  async getCategoryDetail(id: number): Promise<Category> {
    try {
      const response = await get(categoryApi.DETAIL(id));
      return (response.data || response) as unknown as Category;
    } catch (error) {
      console.error(`获取分类详情失败 (ID: ${id}):`, error);
      throw error;
    }
  }

  /**
   * 创建分类
   * @param categoryData 分类数据
   * @returns Promise<Category>
   */
  async createCategory(categoryData: Partial<CategoryBase>): Promise<Category> {
    try {
      const response = await post(categoryApi.CREATE, categoryData);
      return (response.data || response) as unknown as Category;
    } catch (error) {
      console.error('创建分类失败:', error);
      throw error;
    }
  }

  /**
   * 更新分类
   * @param id 分类ID
   * @param categoryData 分类数据
   * @returns Promise<Category>
   */
  async updateCategory(id: number, categoryData: Partial<CategoryBase>): Promise<Category> {
    try {
      const response = await put(categoryApi.UPDATE(id), categoryData);
      return (response.data || response) as unknown as Category;
    } catch (error) {
      console.error(`更新分类失败 (ID: ${id}):`, error);
      throw error;
    }
  }

  /**
   * 更新分类状态
   * @param id 分类ID
   * @param isActive 状态值
   * @returns Promise<Category>
   */
  async updateCategoryStatus(id: number, isActive: number): Promise<Category> {
    try {
      const response = await patch(categoryApi.UPDATE_STATUS(id), { isActive });
      return (response.data || response) as unknown as Category;
    } catch (error) {
      console.error(`更新分类状态失败 (ID: ${id}):`, error);
      throw error;
    }
  }

  /**
   * 批量更新排序
   * @param sortData 排序数据数组
   * @returns Promise<any>
   */
  // async batchUpdateSort(sortData: Array<{ id: number; sort: number }>): Promise<any> {
  //   try {
  //     const response = await patch(categoryApi.BATCH_SORT, sortData);
  //     return response.data || response;
  //   } catch (error) {
  //     console.error('批量更新排序失败:', error);
  //     throw error;
  //   }
  // }

  /**
   * 删除分类
   * @param id 分类ID
   */
  async deleteCategory(id: number): Promise<void> {
    try {
      await del(categoryApi.DELETE(id));
    } catch (error) {
      console.error(`删除分类失败 (ID: ${id}):`, error);
      throw error;
    }
  }

  /**
   * 检查分类是否可删除
   * @param id 分类ID
   * @returns Promise<{ canDelete: boolean; reason?: string }>
   */
  // async checkCategoryDeletable(id: number): Promise<{ canDelete: boolean; reason?: string }> {
  //   try {
  //     const response = await get(categoryApi.CHECK_DELETE(id));
  //     return response.data || response;
  //   } catch (error) {
  //     console.error(`检查分类是否可删除失败 (ID: ${id}):`, error);
  //     throw error;
  //   }
  // }

  /**
   * 获取分类统计数据
   * @returns Promise<Array<{
   *   categoryId: number;
   *   categoryName: string;
   *   articleCount: number;
   *   percentage: number;
   * }>>
   */
//   async getCategoryStats(): Promise<Array<{
//     categoryId: number;
//     categoryName: string;
//     articleCount: number;
//     percentage: number;
//   }>> {
//     try {
//       const response = await get(categoryApi.STATS);
//       return response.data || response;
//     } catch (error) {
//       console.error('获取分类统计数据失败:', error);
//       throw error;
//     }
//   }
}

// 导出分类服务实例
export const categoryService = new CategoryService();
export default categoryService;